#include "qlineeditpwd.h"

#include <QFile>
#include <QHBoxLayout>
#include <QPushButton>
#include <QRegExpValidator>

inline bool LoadStyleFile(QString strFilePath, QString& strContent)
{
    QFile qssFile(strFilePath);
    qssFile.open(QFile::ReadOnly);
    if(!qssFile.isOpen()) {
        return false;
    }

    strContent = qssFile.readAll();
    qssFile.close();

    return true;
}

/*
 * Function:    QLineEditPwd
 * Description: The default constructor
 * Paramer:     void
 * Return:
 */
QLineEditPwd::QLineEditPwd(QWidget *parent) : QLineEdit(parent)
{
    setValidator(new QRegExpValidator(QRegExp("[^\u4E00-\u9FA5]{6,16}")));
    setEchoMode(QLineEdit::Password);
    setMaxLength(20);
    setMinimumHeight(40);

    // Signal-slot initialization
    QPushButton* button = new QPushButton();
    button->setCursor(Qt::PointingHandCursor);
    button->setCheckable(true);
    button->setStyleSheet("QPushButton {background: transparent;}");

    QObject::connect(button, &QPushButton::toggled, [this](bool checked) {
        if (checked) {
            setEchoMode(QLineEdit::Normal);
            setFocus();
        }
        else {
            setEchoMode(QLineEdit::Password);
            setFocus();
        }
    });

    QHBoxLayout* layout = new QHBoxLayout();
    layout->addStretch();
    layout->addWidget(button);
    layout->setContentsMargins(0, 0, 0, 0);
    QWidget::setLayout(layout);
}
